﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;

namespace DoorAndWindow
{
	public partial class DlgNewSize : System.Windows.Forms.Form
	{
		public DlgNewSize(string currentFamilySymbolName, List<string> familySymbols, List<ParameterData> parameterDataList)
		{
			this.InitializeComponent();
			this.tbNewTypeName.Text = currentFamilySymbolName;
			this.m_FamilySymbols.AddRange(familySymbols);
			this.m_ParameterDataList.AddRange(parameterDataList);
			this.InitalizeParameterList();
		}

		public string NewTypeName
		{
			get
			{
				return this.tbNewTypeName.Text.Trim();
			}
		}

		private void DlgNewSize_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			foreach (string strB in this.m_FamilySymbols)
			{
				if (this.NewTypeName.CompareTo(strB) == 0)
				{
					MessageBox.Show("新类型名称已存在，请重新命名。", "盈建科提示");
					e.Cancel = true;
					break;
				}
			}
		}

		private void DGVSize_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			TextBox textBox = e.Control as TextBox;
			if (textBox != null)
			{
				textBox.TextChanged -= this.DGVTextBoxTextChanged;
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void InitalizeParameterList()
		{
			this.DGVSize.RowCount = this.m_ParameterDataList.Count;
			for (int i = 0; i < this.m_ParameterDataList.Count; i++)
			{
				this.DGVSize.Rows[i].Cells[0].Value = this.m_ParameterDataList[i].ParName;
				double num = AssistFunc.feetToMM(this.m_ParameterDataList[i].ParValue);
				this.DGVSize.Rows[i].Cells[1].Value = num.ToString();
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int num = 1;
			if (this.DGVSize.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.DGVSize.CurrentCell.RowIndex;
				string text = ((TextBox)sender).Text;
				double num2 = 0.0;
				try
				{
					num2 = Convert.ToDouble(text);
				}
				catch (Exception)
				{
					double num3 = AssistFunc.feetToMM(this.m_ParameterDataList[rowIndex].ParValue);
					((TextBox)sender).Text = num3.ToString();
					return;
				}
				if (num2 > 0.0)
				{
					this.m_ParameterDataList[rowIndex].ParValue = AssistFunc.mmToFeet(num2);
					this.Refresh();
					return;
				}
				double num4 = AssistFunc.feetToMM(this.m_ParameterDataList[rowIndex].ParValue);
				((TextBox)sender).Text = num4.ToString();
				MessageBox.Show("参数值不能小于等于零", "盈建科提示");
				return;
			}
		}

		private void DlgNewSize_Load(object sender, EventArgs e)
		{
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			char[] array = new char[]
			{
				'\\',
				':',
				'{',
				'}',
				'[',
				']',
				'|',
				';',
				'<',
				'>',
				'?',
				'\'',
				'~',
				'`'
			};
			string newTypeName = this.NewTypeName;
			if (string.IsNullOrEmpty(newTypeName))
			{
				MessageBox.Show("规格名称不能为空", "盈建科提示");
				return;
			}
			foreach (char c in array)
			{
				if (newTypeName.Contains(c))
				{
					MessageBox.Show("规格名称不能包含以下字符： " + c, "盈建科提示");
					return;
				}
			}
			base.DialogResult = DialogResult.OK;
		}

		private List<string> m_FamilySymbols = new List<string>();

		private List<ParameterData> m_ParameterDataList = new List<ParameterData>();
	}
}
